<template>
    <el-dialog v-dialogDrag :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :before-close="closeDailog"
        :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
            label-width="80px">

            <ec-form-item-header value="集团基本信息"></ec-form-item-header>

            <el-row :gutter="15">
                <el-col :span="12">
                    <el-form-item label="简称" prop="shortname">
                        <el-input clearable v-model="dataForm.shortname" placeholder="简称" maxlength="30"
                            show-word-limit></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="名称" prop="name">
                        <el-input clearable v-model="dataForm.name" placeholder="名称" maxlength="30" show-word-limit>
                        </el-input>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row :gutter="15">
                <el-col :span="12">
                    <el-form-item label="上级集团" prop="pkFathergroup">
                        <ec-group-tree-select v-model="dataForm.pkFathergroup" placeholder="上级集团">
                        </ec-group-tree-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="简介" prop="introduction">
                        <el-input clearable v-model="dataForm.introduction" placeholder="简介" maxlength="30"
                            show-word-limit></el-input>
                    </el-form-item>
                </el-col>
            </el-row>

            <ec-form-item-header value="集团扩展信息"></ec-form-item-header>
            <el-row :gutter="15">
                <el-col :span="12">
                    <el-form-item label="电话" prop="tel">
                        <el-input clearable v-model.number="dataForm.tel" placeholder="电话" maxlength="11"
                            show-word-limit></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="传真" prop="fax">
                        <el-input clearable v-model="dataForm.fax" placeholder="传真" maxlength="20" show-word-limit>
                        </el-input>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row :gutter="15">
                <el-col :span="12">
                    <el-form-item label="行政区划" prop="countryarea">
                        <!-- <el-input clearable v-model="dataForm.countryarea" placeholder="行政区划"></el-input> -->
                        <ec-select source="country" v-model="dataForm.countryarea" placeholder="行政区划"></ec-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="国家地区" prop="countryzone">
                        <!-- <el-input clearable v-model="dataForm.countryzone" placeholder="国家地区"></el-input> -->
                        <ec-select source="city" v-model="dataForm.countryzone" placeholder="国家地区"></ec-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="15">
                <el-col :span="12">
                    <el-form-item label="总部地址" prop="headaddress">
                        <el-input clearable v-model="dataForm.headaddress" placeholder="总部地址" maxlength="100"
                            show-word-limit></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12"></el-col>
            </el-row>
            <el-row :gutter="15">
                <el-col :span="12">
                    <el-form-item label="启用状态" prop="enablestate">
                        <ec-dict-select v-model="dataForm.enablestate" placeholder="请选择启用状态" dictType="enable_state">
                        </ec-dict-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12"></el-col>
            </el-row>
            <el-row :gutter="15">
                <el-col :span="12"></el-col>
                <el-col :span="12"></el-col>
            </el-row>


        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="closeDailog">取消</el-button>
            <el-button type="primary" :loading="submitLoading" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import * as groupService from '@/api/upms/group'
    export default {
        data() {
            var checkPhone = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('手机号不能为空'));
                } else {
                    const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
                    if (reg.test(value)) {
                        callback();
                    } else {
                        return callback(new Error('请输入正确的手机号'));
                    }
                }
            }
            return {
                visible: false,
                submitLoading: false,
                dataForm: {
                    pkGroup: 0,
                    pkFathergroup: '',
                    shortname: '',
                    name: '',
                    tel: '',
                    introduction: '',
                    headaddress: '',
                    fax: '',
                    enablestate: 'Y',
                    countryarea: '',
                    countryzone: '',
                    dataoriginflag: '',
                    def1: '',
                    def2: '',
                    def3: '',
                    def4: '',
                    def5: '',
                    revision: '',
                    createdBy: '',
                    createdTime: '',
                    updatedBy: '',
                    updatedTime: ''
                },
                dataRule: {
                    pkFathergroup: [{
                        required: false,
                        message: '上级集团不能为空',
                        trigger: 'change'
                    }],
                    name: [{
                        required: true,
                        message: '名称不能为空',
                        trigger: 'blur'
                    }],
                    headaddress: [{
                        required: false,
                        message: '总部地址不能为空',
                        trigger: 'blur'
                    }],
                    tel: [{
                        required: true,
                        validator: checkPhone,
                        trigger: 'blur'
                    }],
                    fax: [{
                        required: true,
                        message: '传真不能为空',
                        trigger: 'blur'
                    }],
                    enablestate: [{
                        required: false,
                        message: '启用状态',
                        trigger: 'blur'
                    }],
                    countryarea: [{
                        required: false,
                        message: '行政区划不能为空',
                        trigger: 'blur'
                    }],
                    countryzone: [{
                        required: false,
                        message: '国家地区不能为空',
                        trigger: 'blur'
                    }],
                }
            }
        },
        methods: {
            init(id) {
                this.dataForm.pkGroup = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.pkGroup) {
                        groupService.getgroupinfo(this.dataForm.pkGroup).then(
                            data => {
                                this.dataForm.pkFathergroup = data.pkFathergroup
                                this.dataForm.shortname = data.shortname
                                this.dataForm.name = data.name
                                this.dataForm.tel = data.tel
                                this.dataForm.introduction = data.introduction
                                this.dataForm.headaddress = data.headaddress
                                this.dataForm.fax = data.fax
                                this.dataForm.enablestate = data.enablestate
                                this.dataForm.countryarea = data.countryarea
                                this.dataForm.countryzone = data.countryzone
                                this.dataForm.dataoriginflag = data.dataoriginflag
                                this.dataForm.revision = data.revision
                            })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.submitLoading = true
                        var params = {
                            'pkGroup': this.dataForm.pkGroup || undefined,
                            'pkFathergroup': this.dataForm.pkFathergroup,
                            'shortname': this.dataForm.shortname,
                            'name': this.dataForm.name,
                            'tel': this.dataForm.tel,
                            'introduction': this.dataForm.introduction,
                            'headaddress': this.dataForm.headaddress,
                            'fax': this.dataForm.fax,
                            'enablestate': this.dataForm.enablestate,
                            'countryarea': this.dataForm.countryarea,
                            'countryzone': this.dataForm.countryzone,
                            'dataoriginflag': this.dataForm.dataoriginflag,
                            'def1': this.dataForm.def1,
                            'def2': this.dataForm.def2,
                            'def3': this.dataForm.def3,
                            'def4': this.dataForm.def4,
                            'def5': this.dataForm.def5,
                            'revision': this.dataForm.revision,
                            'createdBy': this.dataForm.createdBy,
                            'createdTime': this.dataForm.createdTime,
                            'updatedBy': this.dataForm.updatedBy,
                            'updatedTime': this.dataForm.updatedTime
                        }
                        var tick = !this.dataForm.pkGroup ? groupService.addgroup(params) :
                            groupService.updategroup(params)
                        tick.then(data => {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.submitLoading = false
                                    this.closeDailog()
                                    this.$emit('refreshDataList')
                                }
                            })
                        }).catch((res) => {
                            this.submitLoading = false
                            this.closeDailog()
                        })
                    }
                })
            },
            closeDailog() {
                this.$refs['dataForm'].resetFields()
                this.visible = false
            },
        }
    }
</script>